package com.coffeeloversupportlibrary.coffeelover.networktest;

import android.util.Log;

import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.OriginData;
import com.coffeeloversupportlibrary.coffeelover.networktest.entity.Articles;
import com.coffeeloversupportlibrary.coffeelover.networktest.entity.Beans;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/13 12:38
 * @projectName CoffeeLoverSupportLibrary
 * @className NetModel.java
 * @description TODO
 */
public class NetModel extends BaseModel {
    private List<Article> articles;
    public NetModel(IDataRepository dataRepository) {
        super(dataRepository);
    }
    public void netWorkTest(ViewModelCallBack<Articles> viewModelCallBack){

//        getRetrofitService(ApiService.class).requestTest("283e834c-e314-42f2-9ef3-dca8bb62c07e")
//                .enqueue(new Callback<Articles>() {
//            @Override
//            public void onResponse(Call<Articles> call, Response<Articles> response) {
//                Articles articles=response.body();
//                Log.e("TAG","response");
//                if(articles!=null){
//                    viewModelCallBack.onSuccess(articles);
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Articles> call, Throwable t) {
//                Log.e("TAG","failure");
//            }
//        });

    }



    public void netArticleList(ViewModelCallBack<List<Article>> callback){
        getRetrofitService(ApiService.class)
                .requestArticleList()
                .enqueue(new Callback<OriginData>() {
                    @Override
                    public void onResponse(Call<OriginData> call, Response<OriginData> response) {
                        articles=new ArrayList<>();
                        Log.e("TAG","onresponse");
                        boolean flag=response.body().isSuccessful();
                           if(flag){
                               for(OriginData.DataDTO.ArticleListDTO dto:response.body().getResult()){
                                   Article article=new Article(dto.getArticleId(),dto.getArticleTitle(), dto.getArticleBanner(), dto.getArticleType(), dto.getCreateTime(), dto.getUpdateTime(), dto.getDeleted());
                                   articles.add(article);
                               }
                               callback.onSuccess(articles);
                           }else{
                               callback.Failure();
                           }
                    }
                    //  todo: && bug 原因：数据转换出现问题 走该分支  初始时对数据进行映射实体类进行了修改 感谢博客。。。
                    // todo 经验证 如上推测
                    @Override
                    public void onFailure(Call<OriginData> call, Throwable t) {
                        Log.e("TAG","onFailure.....");
                             callback.Failure();
                    }
                });
    }


    public interface ViewModelCallBack<T>{
        public void onSuccess(T t);
        public void Failure();
    }
}
